import os
import struct

MAX_WORD_LEN = 32

def main():
    os.makedirs('output', exist_ok=True)

    # 'dict.txt', format: Simplified<TAB>Traditional
    entries = []
    with open('input/dict.txt', 'r', encoding='utf-8') as f:
        for line_num, line in enumerate(f, 1):
            line = line.strip()
            if not line or line.startswith('#'):
                continue
            try:
                simplified, traditional = line.strip().split('\t')
                entries.append((simplified, traditional))
            except ValueError:
                print(f'Warning: Line {line_num} is improperly formatted and has been skipped. Content: {line}')
                continue
                
    if not entries:
        print('Error: No valid entries in the dictionary.')
        return

    entries.sort(key=lambda x: x[0])
    with open('output/s2t.idx', 'wb') as idx_file, open('output/s2t.dat', 'wb') as dat_file:
        offset = 0
        for simplified, traditional in entries:
            word_bytes = simplified.encode('utf-8')
            if len(word_bytes) > MAX_WORD_LEN:
                print(f'Warning: Word "{simplified}" exceeds maximum length and has been skipped.')
                continue

            data = traditional + '\n'
            data_bytes = data.encode('utf-8')
            dat_file.write(data_bytes)

            word_bytes = word_bytes.ljust(MAX_WORD_LEN, b'\0')
            idx_file.write(word_bytes)
            idx_file.write(struct.pack('<Q', offset))

            offset += len(data_bytes)

    print('Simplified to Traditional index and data files have been generated.')

    entries.sort(key=lambda x: x[1])
    with open('output/t2s.idx', 'wb') as idx_file, open('output/t2s.dat', 'wb') as dat_file:
        offset = 0
        for simplified, traditional in entries:
            word_bytes = traditional.encode('utf-8')
            if len(word_bytes) > MAX_WORD_LEN:
                print(f'Warning: Word "{traditional}" exceeds maximum length and has been skipped.')
                continue

            data = simplified + '\n'
            data_bytes = data.encode('utf-8')
            dat_file.write(data_bytes)

            word_bytes = word_bytes.ljust(MAX_WORD_LEN, b'\0')
            idx_file.write(word_bytes)
            idx_file.write(struct.pack('<Q', offset))

            offset += len(data_bytes)

    print('Traditional to Simplified index and data files have been generated.')

if __name__ == '__main__':
    main()